var levelList=require("../../utils/level.js");
var md5= require("../../utils/md5.js");
var resourceUrl = getApp().request("imgUrl");
var requestUrl = getApp().request("url");
var header = getApp().request("header");
var config = require('../../utils/parts.js');
var app = getApp().globalData;
var globalOption=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headImgUrl: "",
    nickName: "",
    myRefitTxt:"",
    newPoster:false,
    score:"",
    fightCar:"",
    fightCarMain:"",
    resourceUrl: resourceUrl,
    carLevelStar:"",
    carLevelName:"",
    carName:""
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    globalOption = options;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    this.setData({
      newPoster: false
    });
    var that=this;
    var option = globalOption;
    console.log(option)
    //-----------------------
    var Salt = app.session_key.salt;
    var UserId = app.session_key.userId;
    console.log(Salt&& UserId );
    wx.showLoading({
      title: '',
      mask: true
    });
    if (Salt != "" && UserId != 0) {
      //getApp().showModal("存储进来1111");
       wx.checkSession({
         success:function(ress){
              var accept_salt = Salt;
              var accept_userId = UserId;
              var RefitArr = getApp().Refit();
              var shareId = option.shareId;
              var carTypeId = option.carTypeId;
              var shareRefitNum = option.shareRefitNum
              var carName = option.carName
              var shareToken = md5(md5(shareId + "puzzleidea666") + "puzzleidea666");
              console.log("zhixing1111")
              wx.request({
                url: requestUrl + 'shareCarInfo',
                method: "POST",
                data: { userId: accept_userId, salt: accept_salt, shareId: shareId, shareToken: shareToken, carTypeId: carTypeId },
                header: header,
                success: (res) => {
                  console.log(res)
                  if (res.statusCode != 200) {
                    getApp().showModal("服务器异常");
                    return
                  }
                  if (res.data.code == 200) {
                    var Res = res.data.data;
                    var fightCar = {};       //车件8个框
                    var fightCarMain = {};     //中间的大车
                    for (var key in Res) {
                      if (key.length == 1) {
                        fightCar[key] = Res[key];
                        fightCarMain[key] = Res[key];
                      }
                    }
                    //当车身、轮胎、侧踏板没有选择时使用默认的
                    for (var val in fightCarMain) {
                      if ((val == "b" || val == "c" || val == "g") && fightCarMain[val] == "") {
                        fightCarMain[val] = "0";
                      }
                    };
                    //等级段位
                    var levelArr = levelList.list;
                    var score = Res.score;
                    console.log(score)
                    var levelName = null;
                    for (var i = 0; i < levelArr.length; i++) {
                      if (score >= parseInt(levelArr[i].min) && score <= parseInt(levelArr[i].max)) {
                        levelName = levelArr[i];
                        break;
                      }
                    }
                    console.log(levelArr)
                    console.log(levelName)
                    that.setData({
                      headImgUrl: Res.headimgurl,
                      nickName: Res.nickname,
                      score: Res.score,
                      fightCar: fightCar,
                      fightCarMain: fightCarMain,
                      carLevelStar: levelName.img,
                      carLevelName: levelName.name,
                      myRefitTxt: RefitArr[shareRefitNum],
                      carName: carName
                    });
                    wx.hideLoading();
                  } else if (res.data.code == 800) {
                    getApp().showModal(res.data.data.msg);
                    wx.hideLoading();
                  } else {
                    console.log(res.data.code)
                    getApp().showModal("提交信息有误")
                  }
                  
                },
                fail: (fai) => {
                  console.log("checkSession成功之后获取分享信息fail")
                  console.log(fai)
                  getApp().showModal("网络奔溃啦，请关闭后重试哦！");
                }
              });
             
         },
         fail:function(ress){
           wx.navigateTo({
             url: "/pages/authorize/authorize"
           });
         }
       })
    }else{
      wx.navigateTo({
        url: "/pages/authorize/authorize"
      });
      
    }
    
    //----------------------
    
    
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: getApp().shareTitle1(),
      path: "/pages/index/index"
    }
  }
})